package com.wangzhenyu.learn.test.jetpack.room_

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.room.*
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

//schema 结构
//SQLite 中的表和列名称不区分大小写

@Suppress("UNUSED")
object InstanceRoom {

    @Entity(tableName = "store")
    data class Store(
        @ColumnInfo(name = "store_name")
        var name: String,
        @ColumnInfo(name = "store_type")
        var type: Int = 1,
        @ColumnInfo(name = "store_type_name")
        var typeName: String = "测试店铺",
        @ColumnInfo(name = "store_master")
        var master: String = "WZY"
    ) {
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0

        //忽略字段
        @Ignore
        var time: String = ""
    }

    //类中的两个参数为需要查询的字段
    data class StoreTypeName(
        @ColumnInfo(name = "store_name")
        val name: String,
        @ColumnInfo(name = "store_type_name")
        val typeName: String
    )

    @Entity(tableName = "master")
    data class Master(
        @ColumnInfo(name = "master_name")
        var name: String,
        @ColumnInfo(name = "store_name")
        var storeName: String
    ) {
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0
    }

    data class StoreAndMaster(
        @Embedded val store: Store,
        @Relation(
            parentColumn = "store_name",
            entityColumn = "store_name"
        )
        val master: List<Master>
    )

    //每个 Room 实体都必须定义一个主键
    @Entity(tableName = "word")
    data class Word(
        @ColumnInfo(name = "english_word")
        var word: String = "",
        @ColumnInfo(name = "chinese_word")
        var translate: String = ""
    ) {
        @PrimaryKey(autoGenerate = true)
        var id: Int = 0
    }

    @Dao
    interface StoreDao {
        @Insert
        fun addStore(vararg store: Store): List<Long>

        @Insert
        fun addMaster(vararg master: Master): List<Long>

        @Update
        fun updateStore(vararg store: Store): Int

        @Delete
        fun deleteStore(vararg store: Store): Int

        @Query("SELECT store_name ,store_type_name FROM store where store_name like '%'||:name||'%'")
        fun queryStoreTypeName(name: String): LiveData<List<StoreTypeName>>

        //查询Store与其有关的Master
        @Transaction
        @Query("Select * from store where store_name = :storeName")
        fun queryStoreMaster(storeName: String): List<StoreAndMaster>

    }

    @Dao
    interface WordDao {

        @Insert
        fun insertWords(vararg word: Word)

        @Update
        fun updateWords(vararg word: Word)

        @Delete
        fun deleteWords(vararg word: Word)

        @Query("Delete FROM word")
        fun deleteAllWords()

        @Query("SELECT * FROM word ORDER BY ID DESC")
        fun getAllWords(): LiveData<List<Word>>
    }

    @Database(entities = [Word::class, Store::class, Master::class], version = 3)
    abstract class TestDatabase : RoomDatabase() {

        companion object : com.wangzhenyu.learn.test.designPattern.BaseSingleton<Context, TestDatabase>() {

            override val creator: (Context) -> TestDatabase
                get() = Companion::createWordDatabase

            private fun createWordDatabase(context: Context): TestDatabase {
                return Room.databaseBuilder(context, TestDatabase::class.java, "test_database")
                    .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
                    .build()
            }

            //插入一个新的列
            private val MIGRATION_1_2 = object : Migration(1, 2) {
                override fun migrate(database: SupportSQLiteDatabase) {
                    database.execSQL("ALTER TABLE store ADD COLUMN store_master Text NOT NULL DEFAULT 'WZY'")
                }
            }

            //删除一个列
            private val MIGRATION_2_3 = object : Migration(2, 3) {
                override fun migrate(database: SupportSQLiteDatabase) {
                    database.execSQL(
                        "create table master (id Integer primary key not null," +
                                "master_name Text not null,store_name text not null) "
                    )
                }

            }

        }

        abstract fun getWordDao(): WordDao

        abstract fun getStoreDao(): StoreDao

    }
}